// pages/shopping/subject/subject.js
const app = getApp()
const connector = require('../../../utils/connector.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    arr:[
      { id: "1", name: "科目一", value: false, course:false },
      { id: "2", name: "科目二 ", value: false, course:false },
      { id: "3", name: "科目三", value: false, course:false },
      // { id: "4", name: "科目四", value: false },
    ],
    site:"2",//是否上传场地费发票：1：不传；2：传
    gif:"true",//加载动画

  },
  //选择
  option(e){
    let that = this
    let arr = that.data.arr;
    let course = e.currentTarget.dataset.course
    console.log(course)
    for(let i=0;i<arr.length;i++){
      if (arr[i].id == e.currentTarget.dataset.id && !course){
        arr[i].value = !arr[i].value
      }
    }
    that.setData({
      arr: arr
    })

  },
  //确定选择
  optionBotBtn(e) {
    
    let that = this
    let arr = that.data.arr;
    let arrNew = []
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].value) {
        arrNew.push(arr[i].id)
      }
    }
    console.log(arrNew)
    let arrStr = JSON.stringify(arrNew)
    console.log(arrStr)
    if(arrNew.length < 1){
      wx.showToast({
        title: '请选择科目',
        icon: 'none',
        duration: 1000//持续的时间
      })
      return false
    }
    let site = that.data.site
    wx.navigateTo({
      url: '../uploading/uploading?site=' + site + '&arr=' + arrStr,
    })
    // wx.showModal({
    //   title: '提示',
    //   content: '理赔只能申请一次，请确认您的理赔科目是否选择完全',
    //   success: function (res) {
    //     if (res.confirm) {
    //       // console.log('用户点击确定')
          // wx.navigateTo({
          //   url: '../uploading/uploading?arr=' + arrStr,
          // })
    //     } else if (res.cancel) {
    //       // console.log('用户点击取消')
    //     }
    //   }
    // })
    
    

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // console.log(app.isPlay)
    let that = this
    let arr = []
    // app.isPlay = 1
    if(app.isPlay == 1){
      arr = [
        { id: "1", name: "科目一", value: false,course:false },
        { id: "2", name: "科目二 ", value: false,course:false },
        { id: "3", name: "科目三", value: false,course:false },
      ]
    }
    if(app.isPlay == 2){
      arr = [
        { id: "2", name: "科目二 ", value: false,course:false },
        { id: "3", name: "科目三", value: false,course:false },
      ]
    }
    if(app.isPlay == 3){
      arr = [
        { id: "3", name: "科目三", value: false,course:false },
      ]
    }
    vipStatus(that,arr)

    // that.setData({
    //   arr:arr
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
    var that = this
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },
})
//查询理赔过得科目
function vipStatus(that,arrs){
  connector.getFunc(connector.connector().vipStatus, "", "get", app.tokens, function (res) {
    // console.log(res)
    if (res.statusCode == 200) {
      // console.log(res.data)
      let courseArr = res.data.result
      if(courseArr[0].length > 0){
        for(let i=0;i<arrs.length;i++){
          for(let j=0;j<courseArr[0].length;j++){
            if(arrs[i].id == courseArr[0][j]){
              arrs[i].course = true
            }
          }
        }
      }
      let str = "1"
      if( courseArr[1].parameter.is_upload && (courseArr[1].parameter.is_upload == 1 || courseArr[1].parameter.is_upload == 2)){
        str = courseArr[1].parameter.is_upload
      }
      // console.log(str)
      that.setData({
        arr:arrs,
        gif:"false",
        site:str
      })
    } else {
      wx.showToast({
        title: res.statusCode + ":" + res.errMsg,
        icon: 'none',
        duration: 1000
      })
    }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}